﻿using LanguagePolish.Base;
using LanguagePolish.EntityModel;
using LanguagePolish.Models;
using LanguagePolish.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace LanguagePolish.ViewModels
{
    public class TeachersViewModel : BaseViewModel
    {
        private TeachersModel model;

        private DispatcherTimer timer;
        private string searchPattern;
        public string SearchPattern
        {
            get { return searchPattern; }
            set 
            { 
                searchPattern = value;
                UpdateTeacherCollectionWithWaiting();
                OnPropertyChanged("SearchPattern");
            }
        }

        private ObservableCollection<Teacher> teachers;

        public ObservableCollection<Teacher> Teachers { get { return teachers; } }

        public ICommand AddTeacherCommand { get; private set; }
        public ICommand EditTeacherCommand { get; private set; }
        public ICommand RemoveTeacherCommand { get; private set; }
        public ICommand ShowInfoCommand { get; private set; }

        public TeachersViewModel()
        {
            model = new TeachersModel();
            AddTeacherCommand = new RelayCommand(AddNewTeacher, (o) => model.CanUserAddTeachers);
            EditTeacherCommand = new RelayCommand((o) => EditTeacher((Teacher)o), (o) => model.CanUserEditTeacher);
            RemoveTeacherCommand = new RelayCommand((o) => RemoveTeacher((Teacher)o), (o) => model.CanUserRemoveTeachers);
            ShowInfoCommand = new RelayCommand((o) => ShowTeacherInfo((Teacher)o));
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            timer.Tick += timer_RefreshFilter;

            UpdateTeacherCollection();
        }

        private void UpdateTeacherCollectionWithWaiting()
        {
            timer.Stop();
            timer.Start();
        }

        private void UpdateTeacherCollection()
        {
            var res = model.GetTeachersWithFilter(searchPattern);
            teachers = new ObservableCollection<Teacher>(res);
            OnPropertyChanged("Teachers");
            timer.Stop();
        }

        void timer_RefreshFilter(object sender, EventArgs e)
        {
            UpdateTeacherCollection();
        }

        private void AddNewTeacher()
        {
            Teacher teacher = new Teacher();
            AddTeacherView addTeacherDialog = new AddTeacherView(teacher);
            bool? res = addTeacherDialog.ShowDialog();
            if (res == true)
            {
                teachers.Add(teacher);      // добавляем нового тичера без обновления свойства.
                model.AddTeacher(teacher);
                LinksProvider.Model.CollectionsHasBeenChanged();
            }
        }

        private void EditTeacher(Teacher teacher)
        {
            AddTeacherView addTeacherDialog = new AddTeacherView(teacher, true);
            bool? res = addTeacherDialog.ShowDialog();
            if (res == true)
            {
                teacher.NotifyAll();    // обновляем все свойства
                model.SaveChanges();
                LinksProvider.Model.CollectionsHasBeenChanged();
            }
            else
            {
                model.RollbackTeacher(teacher);     // откат изменений
            }
        }

        private void RemoveTeacher(Teacher teacher)
        {
            MessageBoxResult res = MessageBox.Show("Delete this teacher?", "Attention", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            {
                model.DeleteTeacher(teacher);
                teachers.Remove(teacher);       // из каждой коллекции удаляем отдельно
                LinksProvider.Model.CollectionsHasBeenChanged();
            }
        }

        private void ShowTeacherInfo(Teacher teacher)
        {
            TeacherInfoView infoDialog = new TeacherInfoView(teacher);
            infoDialog.ShowDialog();
        }
    }
}
